<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="org.w3c.dom.Element"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.URL"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%
/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />
<h1 style="border-bottom: 2px dotted #ccc;">Avisos</h1>	
<%

	SimpleDateFormat formatoDB = new SimpleDateFormat("yyyy-MM-dd");
    


	SimpleDateFormat formatoView = new SimpleDateFormat("dd/MM");

	PortletPreferences prefs = renderRequest.getPreferences();
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	dbf.setNamespaceAware(false);
	
	DocumentBuilder docBuilder = dbf.newDocumentBuilder();
	URL avisosURL = new URL("http://10.52.152.2/eventos/avisos.xml?limit=5");
	InputStream isAvisos = avisosURL.openStream();

	Document docAvisos = docBuilder.parse(isAvisos);

	Element avisosTag = docAvisos.getDocumentElement();
	NodeList avisosTags = avisosTag.getElementsByTagName("aviso");
	
	
	
%>






<table cellpadding="10">

<%
	for (int i = 0; i < avisosTags.getLength(); i++) {
		Element avisoTag = (Element) avisosTags.item(i);
		Date dataPublicacao = formatoDB.parse(avisoTag.getElementsByTagName("data_publicacao").item(0).getTextContent());
		String dataPublicacaoStr = formatoView.format(dataPublicacao);
		String dataAtualStr = formatoView.format(new Date());
		session.setAttribute("avisoId", avisoTag.getElementsByTagName("id").item(0).getTextContent());
		//System.out.println(avisoTag.getElementsByTagName("titulo").item(0).getTextContent());
		//System.out.println(eventoTag.getElementsByTagName("titulo").item(0).getTextContent());
%>


	<tr>
	<td align='center' width="70"><img src='/image-proxy/images/clip.png'/></td>
	<td>
	<% if( !avisoTag.getElementsByTagName("texto").item(0).getTextContent().isEmpty()){  %>
			<portlet:renderURL var="detailAvisoURL">
				<portlet:param name="jspPage" value="/detail.jsp"/>
				<portlet:param name="avisoId" value="${avisoId}"/>
			</portlet:renderURL>
			
			
			<a href="<%= detailAvisoURL %>"><%= dataPublicacaoStr %> - <%= avisoTag.getElementsByTagName("titulo").item(0).getTextContent() %></a>
	<% } else { %>
			<% if( !avisoTag.getElementsByTagName("filename").item(0).getTextContent().isEmpty()) { %>
				<a target="_blank" href="http://10.52.152.2/eventos/recursos/<%= avisoTag.getElementsByTagName("filename").item(0).getTextContent() %>"><%= dataPublicacaoStr %> - <%= avisoTag.getElementsByTagName("titulo").item(0).getTextContent() %></a>
			<% }else{ %>
				<%= dataPublicacaoStr %> - <%= avisoTag.getElementsByTagName("titulo").item(0).getTextContent() %>
			<% } %>
			
	<% } %>
	
	
	<% if(dataPublicacaoStr.equals(dataAtualStr)) { %>
		<span style='color:red; text-decoration:blink; font-weight:bold'>&nbsp;(novo)</span>
     <% } %>
     </td>
	</tr>
<%
	}
%>

</table>
